<?php
require_once('simple_html_dom.php');

function topen($url, $timeout = 15, $post = '', $cookie = '', $limit = 0, $ip = '',  $block = TRUE) {
	$return = '';
	$matches = parse_url($url);
	$host = $matches['host'];
	$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
	$port = !empty($matches['port']) ? $matches['port'] : 80;
	if($post) {
		$out = "POST $path HTTP/1.0\r\n";
		$out .= "Accept: */*\r\n";
		//$out .= "Referer: $boardurl\r\n";
		$out .= "Accept-Language: zh-cn\r\n";
		$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8\r\n";
		$out .= "Host: $host\r\n";
		$out .= 'Content-Length: '.strlen($post)."\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Cache-Control: no-cache\r\n";
		$out .= "Cookie: $cookie\r\n\r\n";
		$out .= $post;
	} else {
		$out = "GET $path HTTP/1.0\r\n";
		$out .= "Accept: */*\r\n";
		//$out .= "Referer: $boardurl\r\n";
		$out .= "Accept-Language: zh-cn\r\n";
		$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8\r\n";
		$out .= "Host: $host\r\n";
		$out .= "Connection: Close\r\n";
		$out .= "Cookie: $cookie\r\n\r\n";
	}
	$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
	if(!$fp) {
		return '';
	} else {
		stream_set_blocking($fp, $block);
		stream_set_timeout($fp, $timeout);
		@fwrite($fp, $out);
		$status = stream_get_meta_data($fp);
		if(!$status['timed_out']) {
			while (!feof($fp)) {
				if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
					break;
				}
			}
			$stop = false;
			while(!feof($fp) && !$stop) {
				$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
				$return .= $data;
				if($limit) {
					$limit -= strlen($data);
					$stop = $limit <= 0;
				}
			}
		}
		@fclose($fp);
		return $return;
	}
}

/**
* 转换unicode十进制内码为utf-8编码
*/
 function u2utf8($c) {
	 $str="";
	 if ($c < 0x80) {
	 $str.=$c;
	 } else if ($c < 0x800) {
	 $str.=chr(0xC0 | $c>>6);
	 $str.=chr(0x80 | $c & 0x3F);
	 } else if ($c < 0x10000) {
	 $str.=chr(0xE0 | $c>>12);
	 $str.=chr(0x80 | $c>>6 & 0x3F);
	 $str.=chr(0x80 | $c & 0x3F);
	 } else if ($c < 0x200000) {
	 $str.=chr(0xF0 | $c>>18);
	 $str.=chr(0x80 | $c>>12 & 0x3F);
	 $str.=chr(0x80 | $c>>6 & 0x3F);
	 $str.=chr(0x80 | $c & 0x3F);
	 }
	 return $str;
 }
 
 /**
* 批量转换unicode十进制内码为utf-8编码
* 需要u2utf8()函数支持
*
* 示例：
* $source = "&#20013;&#22269;&#20154;&#27665;";
* echo Du2utf8($source);
*
*
* @param string $source
* @return string
*/
 function Du2utf8($source) {
	 preg_match_all("/&#([0-9]+);/",$source,$regs);
	 foreach($regs[1] as $v) {
	 	$source = str_replace("&#$v;",u2utf8($v),$source);
	 }
	 return  $source;
 }


//用测试
for($page=1;$page<5;$page++){
	$content = topen('http://song.resgain.net/name_list.html?1=1&sex=F&page='.$page);
	echo "crawl $content \n";
	$html = str_get_html($content);
	$items = $html->find('li[style] a');
	foreach($items as $item){
		$fh=fopen('names1.txt','a'); 
		fwrite($fh,Du2utf8($item->innertext)."\n"); 
		fclose($fh); 
	}
	$html->clear();
}





?>